package library.common.util;

import android.text.TextUtils;

/**
 * @author Created by qiang.hou on 2018/10/11.
 * @version 1.0
 */
public class StringUtils {

    /**
     * 计算字符串中有多少个中文字符
     *
     * @param sequence source
     * @return count of Chinese char
     */
    public static int countChineseChar(CharSequence sequence) {

        if (TextUtils.isEmpty(sequence)) {
            return 0;
        }
        int charNum = 0;
        for (int i = 0; i < sequence.length(); i++) {
            char word = sequence.charAt(i);
            if (StringUtils.isChineseChar(word)) {//中文
                charNum++;
            }
        }
        return charNum;
    }


    /**
     * 判断是否是中文
     *
     * @param c source char
     * @return boolean
     */
    public static boolean isChineseChar(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION;
    }

    public static boolean isNull(String str) {
        return str == null || str.equals("") || str.equals("null");
    }
}
